# -*- coding: utf8 -*-

# -*- coding: utf8 -*-
import numpy as np
import matplotlib.pyplot as plt
import pywt
import matplotlib.image as mpimg

def show():
    # Load image
    # original = mpimg.imread('image1.jpg')
    # 加载解密后的照片数据
    original = np.loadtxt("a.txt")
    # 显示加密图片
    # plt.imshow(original, cmap='Greys_r')
    plt.imshow(original, interpolation="nearest", cmap=plt.cm.gray)
    plt.show()
def jie(cip1,cip2,cip3,cip4):
    # 加载解密后的照片数据
    originalImg = mpimg.imread('image.jpg')
    original = np.loadtxt("a.txt")
    # 分割数据源
    arr=np.vsplit(original, 2)
    arr1=np.hsplit(arr[0], 2)
    arr2=np.hsplit(arr[1], 2)


    #将数据源分成小波变换可解析的数据
    LL, (LH, HL, HH)=arr1[0], (arr1[1], arr2[0], arr2[1])
    height=LL.shape[0]
    width=LL.shape[1]

    # 用密文cip1对LL数据解密（可以理解为第一次解密，是对第二次加密的解密）
    # 对加密算法LL[i][j]=LL[i][j] + ((i + j) * 100 * cip1)的反操作
    a = 100
    ll = []
    if (cip1 > 1):
        a = 100
    if (cip1 < 0.0000001):
        a = 100000000
    ll1 = []
    for i in range(0, height):
        tmp = []
        for j in range(0, width):
            tmp.append(LL[i][j]-((i+j)*a*cip1))
        ll1.append(tmp)

    # 用密文cip2对lh数据解密
    lh1 = []
    for i in range(0, height):
        tmp = []
        for j in range(0, width):
            print(i,j)
            tmp.append(LH[i][j]-((i+j)*a*cip2))
        lh1.append(tmp)

    # 用密文cip3对hl数据解密
    hl1 = []
    for i in range(0, height):
        tmp = []
        for j in range(0, width):
            tmp.append(HL[i][j]-((i+j)*a*cip3))
        hl1.append(tmp)

    # 用密文cip4对hh数据解密
    hh1 = []
    for i in range(0, height):
        tmp = []
        for j in range(0, width):
            tmp.append(HH[i][j]-((i+j)*a*cip4))
        hh1.append(tmp)

    # 转换成可用矩阵
    ll1=np.array(ll1)
    lh1=np.array(lh1)
    hl1=np.array(hl1)
    hh1=np.array(hh1)


    # 小波变换解密（可以理解成第二次解密，对第一次加密的解密）
    coeffs22=ll1, (lh1, hl1, hh1)
    # 获取解密后的数据（原图数据）
    reconstructed = pywt.idwt2(coeffs22, 'bior1.3')

    originalImg = rgb2gray(originalImg)
    # originalImg=np.array(originalImg)
    print (originalImg)
    aa=[]
    height1 = LL.shape[0]
    width1 = LL.shape[1]
    for j in range(0, width1):
        tmp=[]
        for i in range(0, height1):
            tmp.append(originalImg[i][j] - reconstructed[i][j])
        aa.append(tmp)
    print (aa)

    # 显示解密后的图片
    fig = plt.figure()
    # 保存解密后的图片
    # plt.imsave('image3.jpg', reconstructed)
    fig = plt.figure()
    fig.suptitle("Decrypt image", fontsize=14)
    plt.imshow(reconstructed, interpolation="nearest", cmap=plt.cm.gray)
    plt.show()


def rgb2gray(rgb):
    return np.dot(rgb[..., :3], [0.299, 0.587, 0.144])
#     不用输入的测试
# show()
# jie(10,10,10,10)